﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using TouchRC.BarManagers;
using TouchRC.Classes;
using TouchRC.MetadataCaching;
using TouchRC.Logic;
using System.IO;
using Microsoft.Drawing;
using System.Drawing;
using Fluid.Controls;
using TouchRC.Custom_Controls;
using TouchRC.Themes;
using MediaScoutGUI.GUITypes;

namespace TouchRC
{
    public class MetadataPanel : FluidPanelBase
    {
        #region Private Members

        private static bool m_ShowPlayButton = true;
        private IMediaSummary m_CurrentMedia = null;
        private AdjustablePanel m_TitleLabel;
        private MetadataDescriptionPanel m_DescriptionPanel;

        #endregion

        public static void ShowMetadataPanel(IMediaSummary mediaSummary, bool showPlayButton)
        {
            m_ShowPlayButton = showPlayButton;
            MetadataPanel panel = new MetadataPanel(mediaSummary);
            panel.ShowMetadata();
        }

        public MetadataPanel(IMediaSummary mediaSummary)
        {
            m_CurrentMedia = mediaSummary;
        }

        public bool ShowPlayButton
        {
            get { return m_ShowPlayButton; }
            set { m_ShowPlayButton = value; }
        }

        #region Fluid Base abstract methods

        public override void SetControlPositions(ScreenOrientation screenOrientation)
        {
            int width = AppSettings.ScreenResolution.Width;
            int height = AppSettings.ScreenResolution.Height;

            m_TitleLabel.Bounds = new Rectangle(0, 0, m_TitleLabel.ScaleX(width), 0);
            m_DescriptionPanel.Bounds = new Rectangle(0, 0, m_DescriptionPanel.ScaleX(width), 0);

            m_TitleLabel.ReadjustHeight();

            m_DescriptionPanel.MaxHeight = height - m_TitleLabel.UnscaleY(m_TitleLabel.Height);

            m_DescriptionPanel.ReadjustHeight();

            m_DescriptionPanel.Top = m_DescriptionPanel.ScaleY(height - m_DescriptionPanel.UnscaleY(m_DescriptionPanel.Height));
        }

        public override void RefreshValues()
        {
            string title = string.Empty;
            string desc = string.Empty;

            if (m_CurrentMedia != null)
            {
                title = m_CurrentMedia.Name;
                desc = m_CurrentMedia.Description;
            }
            
            if (m_TitleLabel.Text != title)
                m_TitleLabel.Text = title;

            m_DescriptionPanel.MaxHeight = AppSettings.ScreenResolution.Height - m_TitleLabel.UnscaleY(m_TitleLabel.Height);

            if (m_DescriptionPanel.Text != title)
                m_DescriptionPanel.Text = desc;
        }

        public override void AddControls()
        {
            Controls.Add(m_TitleLabel);
            Controls.Add(m_DescriptionPanel);
        }

        public override void InitPanelControls()
        {
            m_TitleLabel = new AdjustablePanel();
            m_DescriptionPanel = new MetadataDescriptionPanel();
        }

        public override List<BarOption> GetBarOptions()
        {
            return new List<BarOption>();
        }

        public override void Back()
        {
        }

        #endregion

        protected override void InitControl()
        {
            ShowMenuButton = false;
            ShowHeader = false;

            base.InitControl();

            m_DescriptionPanel.ShowPlayButton = m_ShowPlayButton;
            if (m_ShowPlayButton)
            {
                m_DescriptionPanel.BaseHeight = AppSettings.BaseControlHeight + 5;
            }
            else
            {
                m_DescriptionPanel.BaseHeight = 5;
            }
            m_DescriptionPanel.PlayClick += new EventHandler(m_DescriptionPanel_PlayClick);

            m_TitleLabel.Visible = true;
            m_TitleLabel.LineAlignment = StringAlignment.Center;
            m_TitleLabel.Alignment = StringAlignment.Center;
            m_TitleLabel.Font = Theme.CurrentTheme.HeaderFont;
            m_TitleLabel.ForeColor = Theme.CurrentTheme.HeaderColor;
            m_TitleLabel.BackColor = Color.Black;
            m_TitleLabel.MinHeight = AppSettings.BaseControlHeight;
            m_TitleLabel.BaseHeight = 5;
            m_TitleLabel.Click += new EventHandler(m_DescriptionPanel_BackClick);

            m_TitleLabel.Text = "Unknown Title";
            m_DescriptionPanel.Text = "No Description";
        }

        private void m_DescriptionPanel_BackClick(object sender, EventArgs e)
        {
            CloseMetadata();
        }

        private void m_DescriptionPanel_PlayClick(object sender, EventArgs e)
        {
            if (m_CurrentMedia is MovieSummary)
            {
                MovieSummary bob = m_CurrentMedia as MovieSummary;
                VmcControllerRemote remote = new VmcControllerRemote(MainMenuPanel.CurrentComputer);
                remote.SendFilesToQueue(bob.AssociatedFiles.ToArray());
            }
            CloseMetadata();
        }

        protected override void OnPaintBackground(FluidPaintEventArgs e)
        {
            base.OnPaintBackground(e);
            if (m_CurrentMedia != null)
            {
                if (AppSettings.CurrentOrientation == ScreenOrientation.Landscape &&
                    m_CurrentMedia.Backdrop != null && File.Exists(m_CurrentMedia.Backdrop.FullName))
                {
                    DrawBackgroundImage(e, m_CurrentMedia.Backdrop);
                }
                else if (m_CurrentMedia.Poster != null && File.Exists(m_CurrentMedia.Poster.FullName))
                {
                    DrawBackgroundImage(e, m_CurrentMedia.Poster);
                }
            }
        }

        private void DrawBackgroundImage(FluidPaintEventArgs e, FileInfo file)
        {
            IImagingFactory factory = ImagingFactory.GetImaging();
            IImage image;
            factory.CreateImageFromFile(file.FullName, out image);
            ImageInfo info;
            image.GetImageInfo(out info);
            Rectangle imageDim = GetBackgroundImageDimensions(info, e.ControlBounds);

            e.Graphics.DrawImageAlphaChannel(image, imageDim);
        }

        private Rectangle GetBackgroundImageDimensions(ImageInfo img, Rectangle imageBounds)
        {
            int width = imageBounds.Width;
            int height = imageBounds.Height;
            int x = 0;
            int y = 0;

            double heightScale = (double)img.Height / height;
            double widthScale = (double)img.Width / width;

            if (heightScale < widthScale)
            {
                double ratio = (double)img.Height / img.Width;
                height = (int)(width * ratio);
            }
            else
            {
                double ratio = (double)img.Width / img.Height;
                width = (int)(height * ratio);
            }

            x = x + ((imageBounds.Width - width) / 2);
            y = y + ((imageBounds.Height - height) / 2);

            return new Rectangle(x, y, width, height);
        }

        public void ShowMetadata()
        {
            Show(ShowTransition.FromTop);
            RefreshValues();
            m_DescriptionPanel.ShowAdjustablePanel();
        }

        public void CloseMetadata()
        {
            m_DescriptionPanel.HideAdjustablePanel();
            Close(ShowTransition.FromTop);
        }
    }
}
